#!/bin/bash
# EbbFlow AWS Setup Script

set -e

echo "=== EbbFlow AWS Setup ==="

# 检查AWS CLI是否安装
if ! command -v aws &> /dev/null; then
    echo "AWS CLI not found. Installing..."
    curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
    unzip awscliv2.zip
    sudo ./aws/install
    rm -rf aws awscliv2.zip
fi

# 检查配置
if [ ! -f ~/.aws/credentials ]; then
    echo "AWS credentials not found. Please run 'aws configure' first."
    exit 1
fi

# 创建密钥对
echo "Creating AWS key pair..."
aws ec2 create-key-pair --key-name ebbflow-aws-key --query 'KeyMaterial' --output text > ~/.ssh/ebbflow-key.pem
chmod 400 ~/.ssh/ebbflow-key.pem

# 创建安全组
echo "Creating security group..."
aws ec2 create-security-group \
    --group-name ebbflow-sg \
    --description "Security group for EbbFlow nodes" \
    --query 'GroupId' \
    --output text

# 添加规则
aws ec2 authorize-security-group-ingress \
    --group-name ebbflow-sg \
    --protocol tcp \
    --port 22 \
    --cidr 0.0.0.0/0

aws ec2 authorize-security-group-ingress \
    --group-name ebbflow-sg \
    --protocol tcp \
    --port 11000-11020 \
    --cidr 0.0.0.0/0

echo "AWS setup completed!"
echo "Key pair: ~/.ssh/ebbflow-key.pem"
echo "Security group: ebbflow-sg"
